package com.changgou.common.vo;

import lombok.Data;

import java.io.Serializable;
import java.util.List;
@Data
public class Page implements Serializable{

    //上一页页号
    private Integer upperPageNum;
    //当前页号
    private Integer pageNum;
    //页大小
    private Integer pageSize;
    //总记录数
    private Long total;
    //总页数
    private Integer totalPages;
    //当前页号左右两边的偏移量（间隔当前页多少页）
    private Integer offset = 2;
    //前面3个点
    private Boolean frontDot;
    //后面3个点
    private Boolean backDot;
    //下一页页号
    private Integer nextPageNum;
    //起始页号
    private Integer start;
    //结束页号
    private Integer end;

    public Page(String pageNum, String pageSize, String total, String totalPages) {
        this.pageNum = Integer.parseInt(pageNum);
        this.pageSize = Integer.parseInt(pageSize);
        this.total = Long.parseLong(total);
        this.totalPages = Integer.parseInt(totalPages);

        //初始化
        initPage();
    }

    /**
     * 初始化并计算各个属性
     */
    private void initPage() {
        //处理上下一页
        upperAndNextPageNum();
        //处理要显示的页号数
        handlePageNumList();
        //处理前后3个点
        handleDot();
    }

    /**
     * 处理前后3个点
     */
    private void handleDot() {
        this.frontDot = false;
        if (this.start > 1) {
            this.frontDot = true;
        }
        this.backDot = false;
        if (this.end < this.totalPages) {
            this.backDot = true;
        }
    }

    /**
     * 处理要显示的页号数及前后3个点
     */
    private void handlePageNumList() {
        this.start = 1;
        this.end = this.totalPages;

        //导航条中最多显示的页号数 = 2*偏移量 + 1
        int totalShowPageNum = 2 * this.offset + 1;

        //是否有足够可显示页，只有总页数大于要显示的页数时才有需要计算开始、结束的页号
        if (totalShowPageNum < this.totalPages) {
            this.start = this.pageNum - this.offset;
            this.end = this.pageNum + this.offset;

            if (this.start > 0) {
                if (this.end > this.totalPages) {
                    this.end = this.totalPages;
                    this.start = this.end - totalShowPageNum + 1;
                }
            } else {
                this.start = 1;
                this.end = totalShowPageNum;
            }
        }

    }

    /**
     * 处理上一页页号、下一页页号
     */
    private void upperAndNextPageNum() {
        this.upperPageNum = this.pageNum - 1;
        if (this.upperPageNum < 0) {
            //如果为0，则在页面中不能点击上一页
            this.upperPageNum = 0;
        }

        this.nextPageNum = this.pageNum + 1;
        if (this.nextPageNum > this.totalPages) {
            this.nextPageNum = this.totalPages;
        }
    }
}
